# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import scrapy
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline


class TianyaPipeline(object):
    def process_item(self, item, spider):
        return item


class TianyaImgPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            # 添加meta是为了下面重命名文件名使用
            yield scrapy.Request(image_url, meta={'item': item, 'index': item['image_urls'].index(image_url)})
            # yield scrapy.Request(image_url)

    def item_completed(self, results, item, info):
        print("--------", results)
        # item = results.meta['item']  # 通过上面的meta传递过来item
        # index = results.meta['index']  # 通过上面的index传递过来列表中当前下载图片的下标

        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        item['image_paths'] = image_paths
        return item
